package com.wjjun.base.ui.base;


import android.databinding.ViewDataBinding;
import android.support.v4.app.Fragment;

import com.wjjun.presenter.TabViewPagerPresenter;
import com.wjjun.viewmodel.TabItemViewModel;

/**
 * tabViewPager结合的Activity
 * Created by 67535 on 2018/4/26.
 */

public abstract class TabActivity<VM extends TabViewModel, VDB extends ViewDataBinding> extends BaseActivity<VM, VDB> {

    public TabViewPagerPresenter tabViewPagerPresenter;

    public abstract void initFragment();

    @Override
    protected void initContent() {
        super.initContent();
        initTabViewPager();
        initFragment();
    }

    private void initTabViewPager() {
        tabViewPagerPresenter = new TabViewPagerPresenter(getSupportFragmentManager());
    }

    public void addTab(String title, Fragment fragment) {
        TabItemViewModel itemViewModel = new TabItemViewModel();
        itemViewModel.title = title;
        itemViewModel.fragment = fragment;
        viewModel.addTab(itemViewModel);
    }


    public void addTab(String title, int iconId, Fragment fragment) {
        TabItemViewModel itemViewModel = new TabItemViewModel();
        itemViewModel.title = title;
        itemViewModel.iconId = iconId;
        itemViewModel.fragment = fragment;
        viewModel.addTab(itemViewModel);
    }


    public void addTab(String title, int iconId, String tag, Fragment fragment) {
        TabItemViewModel itemViewModel = new TabItemViewModel();
        itemViewModel.title = title;
        itemViewModel.iconId = iconId;
        itemViewModel.tag = tag;
        itemViewModel.fragment = fragment;
        viewModel.addTab(itemViewModel);
    }


    @SuppressWarnings("ConstantConditions")
    public void renameTab(int index, String title) {
        TabItemViewModel tabItemViewModel = viewModel.getTabItem(index);
        if (tabItemViewModel != null) {
            tabItemViewModel.title = title;
        }
        viewModel.tabViewPagerViewModel.get().tabs.set(index, tabItemViewModel);
    }

    public void switchTab(int position) {
        if (tabViewPagerPresenter != null) {
            tabViewPagerPresenter.switchTab(position);
        }
    }

}
